package cn.kxx.job;


import cn.kxx.config.QiniuUtils;
import cn.kxx.constant.RedisConstant;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.SetOperations;
import org.springframework.scheduling.quartz.QuartzJobBean;

import java.io.Serializable;
import java.util.Set;

/**
 * 定时任务，清理图片
 */
public class ClearImgJob extends QuartzJobBean implements Serializable {
    @Autowired
    private RedisTemplate redisTemplate;
    @Override
    protected void executeInternal(JobExecutionContext jobExecutionContext) throws JobExecutionException {
        System.out.println("开始执行定时任务");
        SetOperations opsForSet = redisTemplate.opsForSet();
        //将垃圾图片放入到另一个set集合中
        opsForSet.differenceAndStore(RedisConstant.UPLOAD_SETMEAL_QNY_KEY,
                RedisConstant.UPLOAD_SETMEAL_DB_KEY,RedisConstant.UPLOAD_SETMEALIMG_DIFF_KEY);
        // TODO 清除垃圾图片
        Set<String> members = opsForSet.members(RedisConstant.UPLOAD_SETMEALIMG_DIFF_KEY);
        System.out.println("=============>>>>members="+members);
        for (String member : members) {
            //在七牛云中删除此图片
            QiniuUtils.deleteFileFromQiniu(member);
            //删除差集中的元素
            opsForSet.remove(RedisConstant.UPLOAD_SETMEALIMG_DIFF_KEY,member);
            //删除七牛云缓存中的元素
            opsForSet.remove(RedisConstant.UPLOAD_SETMEAL_QNY_KEY,member);
        }

    }
}